%read frequency in gates file from flowjo tables
%Array: [row,col,rep,gate]
flow_path = '../All_Lim_FACS_tvs/111112_freq_gates_OD_DOX.tab';

data = read_flowjo_table(flow_path);

%% Plotting
defaultPlot()
%OD_sweep: Calculate OD ratio and %clustering
r_to_g = squeeze(data(:,1,:,1)./data(:,1,:,3));
g_to_r = 1./r_to_g;
r_to_g(1,:)=0;
g_to_r(8,:)=0;
g = [.8 .4 .2 .1 .05 .025 .0125 0];
r = repmat(.1,1,8);
ratio_calc = g./r;
total_od = g+r;
clust_pct_od = squeeze(data(:,2,:,2));
d_clust_pct_od = squeeze(data(:,2,:,2)./data(:,1,:,2));
%precise, no error bars
plot(log10(g_to_r),clust_pct_od)
defaultPlot()
xlabel('log_1_0 OD Ratio ')
ylabel('% Cells Clustered')
%less precise, calculated od
figure, defaultPlot()
plot(log10(ratio_calc),clust_pct_od)
defaultPlot()
xlabel('log_1_0 OD Ratio ')
ylabel('% Cells Clustered')
%errorbar, parabolic
figure, defaultPlot()
errorbar(log10(ratio_calc),nanmean(clust_pct_od,2),nanstd(clust_pct_od,0,2)/sqrt(4))
defaultPlot()
xlabel('log_1_0 OD Ratio ')
ylabel('% Cells Clustered')
legend('n = 4')
%but total OD is monotonically increasing
figure, defaultPlot()
plot(log10(ratio_calc),total_od,'r--')
defaultPlot()
xlabel('log_1_0 OD Ratio ')
ylabel('Total OD')

dox = repmat(100,1,16)./repmat(2,1,16).^((1:16)-1);
dox(16) = 0;
clust_pct_dox = cat(1,squeeze(data(:,5,:,2)),squeeze(data(:,6,:,2)));
minus_dox = repmat(data(8,6,:,2),[8 1 1 1]);
ratio_minus_dox = repmat(data(8,6,:,3)./data(8,6,:,1),[8 1 1 1]);
d_clust_pct_dox = cat(1,squeeze(data(:,5,:,2)./minus_dox),squeeze(data(:,6,:,2)./minus_dox));
%Best metric:
od_norm_clust_pct_dox = cat(1,squeeze(data(:,5,:,2)./ratio_minus_dox),squeeze(data(:,6,:,2)./ratio_minus_dox));
figure, defaultPlot()
errorbar(log10(dox),nanmean(od_norm_clust_pct_dox,2),nanstd(od_norm_clust_pct_dox,0,2)/sqrt(3));
defaultPlot()
xlabel('log_1_0 [Dox], \mug/mL')
ylabel('% Cells Clustered')
legend('n = 3')